package com.green.voluntaryfiling.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.green.voluntaryfiling.common.DataGridView;
import com.green.voluntaryfiling.common.ResultObj;
import com.green.voluntaryfiling.domain.VolunteerFilled;
import com.green.voluntaryfiling.service.VolunteerFilledService;
import com.green.voluntaryfiling.vo.VoluntaryFillingVo;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: Green
 * @Date: 2022/04/04/15:48
 * @Description:
 */

@RequestMapping("voluntary")
@RestController
public class VoluntaryController {


    @Autowired
    private VolunteerFilledService volunteerFilledService;

    /**
     * 更新、修改
     * @param voluntaryFilling
     * @return
     */
    @RequestMapping("voluntaryfilling")
    public ResultObj voluntaryfilling(String voluntaryFilling){
        JSONObject jsonObject = JSONObject.fromObject(voluntaryFilling);
        VoluntaryFillingVo voluntaryFillingVo = (VoluntaryFillingVo)JSONObject.toBean(jsonObject,VoluntaryFillingVo.class);
        try {
            if (voluntaryFillingVo.getType().equals(1)){
                //更新
                this.volunteerFilledService.updateRecord(voluntaryFillingVo);
                return ResultObj.VOLUNTRAY_FILL_UPDATE_SUCCESS;
            }else {
                //提交
                this.volunteerFilledService.saveRecord(voluntaryFillingVo);
                return ResultObj.VOLUNTRAY_FILL_SUCCESS;
            }
        }catch (Exception e){
            e.printStackTrace();
            return ResultObj.VOLUNTRAY_FILL_ERROR;
        }
    }

    /**
     * 查询
     * @param username
     * @return
     */
    @RequestMapping("volunteerList")
    public ResultObj volunteerList(String username){
        try {
            DataGridView dataGridView = this.volunteerFilledService.queryALLInfoFormUser(username);
            if (dataGridView.getCount().equals(0)){
                return ResultObj.VOLUNTARY_QUERY_FAIL;
            }else{
                return new ResultObj(ResultObj.VOLUNTARY_QUERY_SUCCESS_CODE,ResultObj.VOLUNTARY_QUERY_SUCCESS_MSG,
                        dataGridView);
            }
        }catch (Exception e){
            e.printStackTrace();
            return ResultObj.VOLUNTARY_QUERY_ERROR;
        }
    }

    @RequestMapping("deleteVolunteer")
    public ResultObj deleteVolunteer(String username, Integer university_order_id){
        try {
            QueryWrapper<VolunteerFilled> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq(StringUtils.isNotBlank(username),"username",username);
            queryWrapper.eq(university_order_id!=null,"university_order_id",university_order_id);
            if (this.volunteerFilledService.remove(queryWrapper)){
                return ResultObj.VOLUNTARY_DELETE_SUCCESS ;
            }else {
                return ResultObj.VOLUNTARY_DELETE_FAIL;
            }
        }catch (Exception e ){
            e.printStackTrace();
            return ResultObj.VOLUNTARY_DELETE_ERROR;
        }
    }
}
